package com.situ.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Area;
import com.situ.web.entity.City;
import com.situ.web.entity.Province;
import com.situ.web.service.IAreaService;
import com.situ.web.service.impl.AreaServiceImpl;

import net.sf.json.JSONArray;

public class SelectAreaServlet extends HttpServlet {
	private IAreaService areaService = new AreaServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "province":
			province(req,resp);
			break;
		case "city":
			city(req,resp);
			break;
		case "area":
			area(req,resp);
			break;

		default:
			break;
		}
	}

	private void area(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String cityid = req.getParameter("cityid");
		ArrayList<Area> list = areaService.findAllArea(Integer.parseInt(cityid));
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
		
	}

	private void city(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String provinceId = req.getParameter("provinceId");
		List<City> list = areaService.findCity(Integer.parseInt(provinceId));
		for (City city : list) {
			System.out.println(city);
		}
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
		
	}

	private void province(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<Province> list = areaService.findlist();
		System.out.println("======================");
		System.out.println(list);
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());	
	}
}
